﻿using System;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using MyAMS.Utility;
using MyAMS.WebServer.ActionFilter;
using MyAMS.WebServer.Routing;

namespace MyAMS.WebServer
{
    class CMSModule : BaseModule
    {
        private static readonly string assemblyName = Assembly.GetExecutingAssembly().FullName.Split(',')[0];
        private readonly IUnityContainer container;

        public CMSModule(IUnityContainer container)
        {
            string configFilePath = String.Format(IUnityContainerExtensions.unityConfigurationFilePath, assemblyName);
            this.container = container.Configure(HttpContext.Current.Server.MapPath(configFilePath));
        }

        public override string Name
        {
            get { return "Modules.CMS"; }
        }

        public override void LoadCatchAllRoutes(RouteCollection routes)
        {
            string[] controllerNamespaces = new string[] { "MyAMS.WebServer.Controllers" };

            routes.MapRoute(
                "Node",
                "{*nodeData}",
                new { controller = "Node", action = "Details" },
                new { NodeData = container.Resolve<NodeConstraint>() },
                controllerNamespaces
                );

            routes.MapRoute(
                "Archive",
                "{*archiveData}",
                new { controller = "Archive", action = "Details" },
                new { archiveData = container.Resolve<ArchiveConstraint>() },
                controllerNamespaces
                );
        }

        public override void UnLoadRoutes(RouteCollection routes)
        {
            //TODO (5he11)
        }

        public override void LoadViewEngines(ViewEngineCollection viewEngines)
        {
            viewEngines.Add(container.Resolve<IViewEngine>());
        }

        public override void UnLoad()
        {
            //TODO (5he11)
            base.UnLoad();
        }

        public override void LoadActionFilters(IActionFilterRegistry filterRegistry)
        {
            //ControllerActionCriteria listActionsCriteria = new ControllerActionCriteria();
            //listActionsCriteria.AddMethod<NodeController>(a => a.Details(null));
            //filterRegistry.Register(new[] { listActionsCriteria }, typeof(AuthorizationFilter));
        }
    }
}
